package com.wo.cloud.admin.listener;

import com.wo.cloud.admin.entity.SysLog;
import com.wo.cloud.admin.service.ISysLogService;
import com.wo.cloud.kafka.channel.LogChannel;
import com.wo.cloud.core.entity.CommonLog;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/5/10 10:44
 * Description: 消息存储到数据库中
 */

// 绑定通道
@Component
public class LogMessageListener {

    @Autowired
    private ISysLogService sysLogService;

    /**
     * 消息消费
     *
     * @param commonLog
     */
    @StreamListener(LogChannel.LOG_INPUT)
    public void handler(@Payload CommonLog commonLog) {
        // 将消息输出到日志中或者入库
        SysLog sysLog = new SysLog();
        BeanUtils.copyProperties(commonLog, sysLog);
        sysLogService.save(sysLog);
    }
}
